package ywh.model
{
	import fl.events.SliderEvent;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import ywh.ui.SliderForContent;
	
	/**
	 * ...
	 * @author 杨伟华
	 */
	public class Model2TextPanel extends Sprite
	{
		private var _panelSlider:Sprite;
		
		private var _infoTf:TextField;
		
		private var _content:Sprite;
		
		private var _photo:Loader;
		
		private var _mask:Sprite;
		
		private var _width:Number;
		
		private var _height:Number;
		
		private var _pwidth:Number;
		
		private var padding:Number = 10;
		
		private var _contentInitY:Number;
		
		private var _contentHeight:Number;
		
		private var _maxScroll:Number;
		
		private var _currentPosition:Number;
		
		private var _photoUrl:String;
		
		private var _showPhoto:Boolean;
		
		private var _mouseDownY:Number;
		
		private var _mouseDownContentY:Number;
		
		public function Model2TextPanel()
		{
			_mask = new Sprite();
			addChild(_mask);
			_content = new Sprite();
			_content.mask = _mask;
			addChild(_content);
			_photo = new Loader();
			addChild(_photo);
			_panelSlider = new SliderForContent();
			initInfoTf();
			initSlider();
		}
		
		private function initInfoTf():void
		{
			var tf:TextFormat = new TextFormat();
			tf.size = 14;
			tf.color = 0x006600;
			tf.leading = 15;
			_infoTf = new TextField();
			_infoTf.y = padding;
			_infoTf.defaultTextFormat = tf;
			_infoTf.condenseWhite = true;
			_infoTf.multiline = true;
			_infoTf.wordWrap = true;
			_infoTf.selectable = false;
			_infoTf.autoSize = TextFieldAutoSize.LEFT;
			_infoTf.mouseWheelEnabled = false;
			_content.addChild(_infoTf);
			_content.addEventListener(MouseEvent.MOUSE_DOWN, content_mouseDown);
		}
		
		private function content_mouseDown(e:MouseEvent):void
		{
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMove);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, stage_mouseUp);
			_mouseDownY = e.stageY;
			_mouseDownContentY = _content.y;
		}
		
		private function stage_mouseUp(e:MouseEvent):void
		{
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMove);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, stage_mouseUp);
		}
		
		private function stage_mouseMove(e:MouseEvent):void
		{
			var targetY:Number = e.stageY - _mouseDownY + _mouseDownContentY;
			if (targetY > 0)
			{
				_content.y = 0;
			}
			else if (targetY < -_maxScroll)
			{
				_content.y = -_maxScroll;
			}
			else
			{
				_content.y = targetY;
			}
			var panelSlider:SliderForContent = _panelSlider as SliderForContent;
			panelSlider.value = -_content.y / _maxScroll * panelSlider.maximum;
		}
		
		private function initSlider():void
		{
			var panelSlider:SliderForContent = _panelSlider as SliderForContent;
			panelSlider.minimum = 0;
			panelSlider.maximum = 100;
			panelSlider.snapInterval = 1;
			panelSlider.addEventListener(SliderEvent.CHANGE, panelSlider_change);
			panelSlider.rotation = 90;
			addChild(panelSlider);
		}
		
		private function panelSlider_change(e:Event):void
		{
			var _e:SliderEvent = e as SliderEvent;
			var panelSlider:SliderForContent = _panelSlider as SliderForContent;
			_content.y = 0 - _maxScroll * panelSlider.value / panelSlider.maximum;
		}
		/**
		 * 设置尺寸
		 * @param	w 宽度
		 * @param	h 高度
		 * @param	pw 照片尺寸
		 */
		public function setSize(w:Number, h:Number, pw:Number):void
		{
			_width = w;
			_height = h;
			_pwidth = pw;
			_mask.graphics.clear();
			_mask.graphics.beginFill(0xff0000);
			_mask.graphics.drawRect(0, 0, w, h);
			_mask.graphics.endFill();
			_infoTf.width = w - padding * 2;
			_panelSlider.x = width-3;
			_panelSlider.y = 52;
			_panelSlider.width = height - 90;
		}

		public function setText(text:String, photourl:String)
		{
			var showPH:Boolean = Boolean(photourl);
			_infoTf.parent && _content.removeChild(_infoTf);
			_infoTf.htmlText = "";
			if (_showPhoto != showPH)
			{
				_showPhoto = showPH;
				if (_showPhoto)
				{
					if (_photoUrl != photourl)
					{
						_photoUrl = photourl;
						_photo.load(new URLRequest(photourl));
					}
					_photo.visible = true;
					_infoTf.x = _pwidth + padding * 2;
					_infoTf.width = _width - _pwidth - padding * 3;
				}
				else
				{
					_photo.visible = false;
					_infoTf.x = padding;
					_infoTf.width = _width - padding * 2;
				}
			}
			_content.y = 0;
			_infoTf.htmlText = text;
			_content.addChild(_infoTf);
			_contentHeight = _infoTf.textHeight + padding * 2;
			_maxScroll = _contentHeight - _height;
			_panelSlider.visible = _maxScroll > 0;
			(_panelSlider as SliderForContent).value = 0;
		}
	}
}